package com.sl.permission.util

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.sl.permission.annotation.PermissionDenied
import com.sl.permission.annotation.PermissionDeniedForever
import java.util.*
import kotlin.reflect.KClass

/**
 * author： suliang
 * 2022/9/29 11:22
 * describe : 权限工具类
 */
class PermissionUtil {

    companion object{

        /**
         * Has permission request
         * 判断权限是否都申请通过了
         * @param context
         * @param permissions
         * @return true 通过了， false 没有通过
         */
        @JvmStatic
        fun hasPermissionRequest(context: Context,permissions : Array<out String>):Boolean{
            var result = true
            run m@{
                permissions.forEach {
                    if (ContextCompat.checkSelfPermission(context,it) != PackageManager.PERMISSION_GRANTED){
                        result = false
                        return@m
                    }
                }
            }
            return result
        }

        /**
         * Should show request permission rationale
         * 用户是否拒绝并点击了不再提示
         * @param context
         * @param permissions
         * @return
         */
        fun shouldShowRequestPermissionRationale(context: Activity, permissions: Array<out String>) : Boolean{
            var result = false
            run m@{
                permissions.forEach {
                    if (!ActivityCompat.shouldShowRequestPermissionRationale(context,it)){
                        result = true
                        return@m
                    }
                }
            }
            return result
        }

        /**
         * Invoke annotation
         * 执行被注解的方法
         * @param any  切入点所在的对象
         * @param annotation 注解的类型
         * @param requestCode
         */
        fun invokeAnnotation(any: Any, annotation:Class<out Annotation>,requestCode : Int){
            val objectClass = any.javaClass     //所在类
            val declaredMethods = objectClass.declaredMethods  // 所有方法
            run m@{
                declaredMethods.forEach { method ->
                    method.isAccessible = true      //让虚拟机不检测private
                    //是否注解方法
                    if (method.isAnnotationPresent(annotation)){
                        try {
                            val annotationObject = method.getAnnotation(annotation)
                            //获取注解的requestCode值
                            val annotationRequestCode = when(annotation){
                                PermissionDenied::class.java -> (annotationObject as PermissionDenied).requestCode
                                PermissionDeniedForever::class.java -> (annotationObject as PermissionDeniedForever).requestCode
                                else -> 0
                            }
                            if (annotationRequestCode == requestCode){
                                method.invoke(any)
                                return@m
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                    }
                }
            }
        }

        /**
         * Request permission success
         * 检查权限是否都成功了
         * @param grantResults
         * @return
         */
        fun requestPermissionSuccess(grantResults: IntArray): Boolean {
            if (grantResults.isEmpty()){
                return false
            }
            grantResults.forEach {
                if (it != PackageManager.PERMISSION_GRANTED){
                    return false
                }
            }
            return true
        }
    }
}